package org.github.rayson517.easyplatform.common.web.config.druid;

import java.sql.SQLException;

import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * 
 * <p>Title: DruidProperties</p>
 * <p>Description: 连接池配置</p>
 * @author rayson517
 * @date 2018-06-07 11:52
 * @version 1.0
 */
@Configuration
public class DruidProperties implements EnvironmentAware {
    
     private RelaxedPropertyResolver propertyResolver;  
      
     public void setEnvironment(Environment env) {  
         this.propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource.");  
     }  
     public void config(DruidDataSource datasource) {
         datasource.setUrl(propertyResolver.getProperty("url"));  
         datasource.setDriverClassName(propertyResolver.getProperty("driver-class-name"));  
         datasource.setUsername(propertyResolver.getProperty("username"));  
         datasource.setPassword(propertyResolver.getProperty("password"));  
         datasource.setInitialSize(Integer.valueOf(propertyResolver.getProperty("initialSize")));  
         datasource.setMinIdle(Integer.valueOf(propertyResolver.getProperty("minIdle")));  
         datasource.setMaxWait(Long.valueOf(propertyResolver.getProperty("maxWait")));  
         datasource.setMaxActive(Integer.valueOf(propertyResolver.getProperty("maxActive")));  
         datasource.setMinEvictableIdleTimeMillis(  
                Long.valueOf(propertyResolver.getProperty("minEvictableIdleTimeMillis")));  
         try {  
            datasource.setFilters("stat,wall");  
         } catch (SQLException e) {  
            e.printStackTrace();  
         }  
    }
}